package com.cw.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cw.data.ConstantEnum;
import com.cw.data.PageBean;
import com.cw.data.Result;
import com.cw.pojo.TbSeller;
import com.cw.pojo.TbStatus;
import com.cw.service.SellerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("sellerController")
public class SellerController {

    @Reference
    private SellerService sellerService;

    @RequestMapping("toSeller")
    public String toSeller(Model model) {

        List<TbStatus> statusList = sellerService.getList();

        model.addAttribute("status", statusList);

        return "admin/seller";
    }

    @RequestMapping("getSellerInfo")
    @ResponseBody
    public Result getSellerInfo(@RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "3") Integer pageSize, String name, String nickName, String status) {

        try {

            PageBean<TbSeller> pa = sellerService.getSellerInfo(pageNumber, pageSize, name, nickName, status);

            return Result.result(ConstantEnum.SUCCESS).setData(pa);

        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("getOneSellerInfo")
    @ResponseBody
    public Result getOneSellerInfo(String sellerId) {

        try {

            TbSeller tbSeller = sellerService.getOneSellerInfo(sellerId);

            return Result.result(ConstantEnum.SUCCESS).setData(tbSeller);

        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("setStatus")
    @ResponseBody
    public Result setStatus(String status, String sellerId) {

        try {

            sellerService.setStatus(status, sellerId);

            return Result.result(ConstantEnum.SUCCESS);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }
}
